﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Ahp;

namespace AhpTest
{
    [TestClass]
    public class GoalNodeTest
    {
        [TestMethod]
        public void InitialWeightTest()
        {
            GoalNode goalNode = new GoalNode();
            Assert.AreEqual(1M, goalNode.LocalPriority, "Initial LocalPriority value must be 1");
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void LocalPrioritySetterTest()
        {
            GoalNode goalNode = new GoalNode();
            goalNode.LocalPriority = 2M;
        }

        #region Criterion nodes manipulations

        [TestMethod]
        public void AddCriterionNodeTest()
        {
            GoalNode goalNode = new GoalNode();
            CriterionNode criterionNode = new CriterionNode();

            goalNode.CriterionNodes.Add(criterionNode);
            Assert.AreEqual(goalNode, criterionNode.GoalNode);
        }

        [TestMethod]
        public void RemoveCriterionNodeTest()
        {
            GoalNode goalNode = new GoalNode();
            CriterionNode criterionNode = new CriterionNode();

            goalNode.CriterionNodes.Add(criterionNode);
            Assert.AreEqual(goalNode, criterionNode.GoalNode);

            goalNode.CriterionNodes.Remove(criterionNode);
            Assert.IsNull(criterionNode.GoalNode);
        }

        [TestMethod]
        public void ClearCriterionNodesTest()
        {
            GoalNode goalNode = new GoalNode("Goal");
            CriterionNode criterionNode1 = new CriterionNode("Criterion1");
            CriterionNode criterionNode2 = new CriterionNode("Criterion2");
            goalNode.CriterionNodes.Add(criterionNode1);
            goalNode.CriterionNodes.Add(criterionNode2);

            goalNode.CriterionNodes.Clear();
            Assert.IsNull(criterionNode1.GoalNode);
            Assert.IsNull(criterionNode2.GoalNode);
        }

        #endregion

        [TestMethod]
        public void GetLowestCriterionNodesTest()
        {
            GoalNode goalNode = new GoalNode();

            //First level criterions
            CriterionNode criterion1 = new CriterionNode("Criterion1");
            goalNode.CriterionNodes.Add(criterion1);

            CriterionNode criterion2 = new CriterionNode("Criterion2");
            goalNode.CriterionNodes.Add(criterion2);

            CriterionNode criterion3 = new CriterionNode("Criterion3");
            goalNode.CriterionNodes.Add(criterion3);

            //Second level criterions
            CriterionNode criterion11 = new CriterionNode("Criterion11");
            criterion1.SubcriterionNodes.Add(criterion11);

            CriterionNode criterion12 = new CriterionNode("Criterion12");
            criterion1.SubcriterionNodes.Add(criterion12);

            CriterionNode criterion21 = new CriterionNode("Criterion21");
            criterion2.SubcriterionNodes.Add(criterion21);

            //Third level criterions
            CriterionNode criterion111 = new CriterionNode("Criterion111");
            criterion11.SubcriterionNodes.Add(criterion111);

            CriterionNode criterion112 = new CriterionNode("Criterion112");
            criterion11.SubcriterionNodes.Add(criterion112);

            CriterionNode criterion121 = new CriterionNode("Criterion121");
            criterion12.SubcriterionNodes.Add(criterion121);

            CriterionNodeCollection lowestCriterionNodes = goalNode.GetLowestCriterionNodes();
            Assert.AreEqual(5, lowestCriterionNodes.Count);
            Assert.IsTrue(lowestCriterionNodes.Contains(criterion111));
            Assert.IsTrue(lowestCriterionNodes.Contains(criterion112));
            Assert.IsTrue(lowestCriterionNodes.Contains(criterion121));
            Assert.IsTrue(lowestCriterionNodes.Contains(criterion21));
            Assert.IsTrue(lowestCriterionNodes.Contains(criterion3));
        }
    }
}
